package org.light.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.light.domain.Domain;
import org.light.domain.Dropdown;
import org.light.domain.Controller;
import org.light.domain.Field;
import org.light.domain.FieldComparator;
import org.light.domain.Method;
import org.light.domain.ValidateInfo;
import org.light.exception.ValidateException;
import org.light.utils.DomainUtil;
import org.light.utils.StringUtil;

public class GinPlainController extends GinController implements Comparable<Controller>{
	private static final long serialVersionUID = 6506582842158817100L;

	public GinPlainController(Domain domain) throws ValidateException {
		super(domain);
		this.domain = domain;
	}

	@Override
	public String generateControllerString() throws ValidateException {
		StringBuilder sb = new StringBuilder();
		sb.append("package ").append(this.getDomain().getControllerSuffix()).append("\n\n");
		Set<String> imports = this.generateImportStrings();
		imports.add("github.com/gin-gonic/gin");
		imports.add("session \"github.com/ScottHuangZL/gin-jwt-session\"");
		
		imports.add(DomainUtil.packagetokenToGoPackageFolder(this.domain.getProjectName(),this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()));
		imports.add(DomainUtil.packagetokenToGoPackageFolder(this.domain.getProjectName(),this.domain.getPackageToken()+"."+this.domain.getServiceimplSuffix()));
		imports.addAll(this.classImports);
		String importsStr = DomainUtil.gemerateImportStr(imports);
		sb.append(importsStr);
		sb.append("\n");
		Set<Field> myFields = new TreeSet<Field>(new FieldComparator());
		sb.append("\n");
		myFields.addAll(this.getFields());
		// generate fields notions
		for (Field f : myFields) {
			sb.append("\tvar ").append(f.getLowerFirstFieldName()).append(" ").append(f.getFieldType()).append(" ");
			if (f.getFieldValue() != null) {
				if (!f.getFieldType().equals("String")){
					sb.append(" = ").append(f.getFieldValue());
				}else {
					sb.append(" = \"").append(f.getFieldValue()).append("\"");
				}
			}
			sb.append("\n");
		}
		sb.append("\n");

		Iterator it2 = this.getMethods().iterator();
		while (it2.hasNext()) {
			sb.append(((Method) it2.next()).generateMethodString()).append("\n");
		}
		return sb.toString();
	}

	public ValidateInfo validate() {
		ValidateInfo info = new ValidateInfo();
		info.setSuccess(true);
		if (this.verbs == null || this.verbs.size() == 0) {
			info.setSuccess(false);
			info.addCompileError("Controller " + this.standardName
					+ " 动词是空！");
		}
		for (Verb v : this.verbs) {
			if (v != null && v.getDomain() == null) {
				info.setSuccess(false);
				info.addCompileError("Controller " + this.standardName + "动词"
						+ v.getVerbName() + "域对象是空值！");
			}
		}
		return info;
	}

	public GinPlainController(List<Verb> verbs, Domain domain,Boolean ignoreWarning)
			throws ValidateException {
		super(domain);
		this.domain = domain;
		try{
			for (Verb v : verbs) {
				v.setDomain(domain);
			}
			this.verbs = verbs;
			this.standardName = domain.getCapFirstDomainName() + domain.getControllerNamingSuffix();
	
			ValidateInfo info = this.validate();
			if (info.success(ignoreWarning)) {
				for (Field f:domain.getFields()) {
					if (f instanceof Dropdown) {
						Dropdown dp = (Dropdown)f;
					}
				}
			} else {
				ValidateException e = new ValidateException(info);
				throw e;
			}
		}catch (Exception ex){
			ValidateInfo info = new ValidateInfo();
			info.addCompileError("GinPlain控制器新建失败！");
			ValidateException e = new ValidateException(info);
			throw e;
		}
	}
}
